const util = require("../../../../utils/util")

// pages/classHomePage/discuss/discussMessage/discussMessage.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    discuss_id: null,
    discuss_message: null,
    reply: null,
    input_reply:null,
    timelock:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this
    if (options != null) {
      console.log('页面跳转传值：', options)
      _this.setData({
        discuss_id: options['id']
      })
    }
    _this.get_discuss_message()
  },

  get_discuss_message: function () {
    let _this = this
    let data = {
      'postId': _this.data.discuss_id
    }
    app.formPost('/api/admin/post/getPostWithReplies/{id}?', data).then(res => {
      console.log('查看讨论和回复：', res);
      _this.setData({
        discuss_message: res
      })
    })
  },

  inputReply: function (e) {
    this.setData({
      reply: e.detail.value
    })
  },

  sendReply: function () {
    let _this = this
    let data = {}
    if (wx.getStorageSync('people') == 'student') {
      data = {
        'postId': _this.data.discuss_id,
        'replyContent': _this.data.reply,
        'student_account': app.globalData.student_info['account']
      }
      app.formPost('/api/admin/post_reply/student/add_reply', data).then(res => {
        console.log('回复结果：', res);
        _this.setData({
          input_reply: null
        })
        _this.onLoad()
      })
    } else if (wx.getStorageSync('people') == 'teacher') {
      data = {
        'postId': _this.data.discuss_id,
        'replyContent': _this.data.reply,
        'teacher_account': app.globalData.teacher_info['account']
      }
      app.formPost('/api/admin/post_reply/teacher/add_reply', data).then(res => {
        console.log('回复结果：', res);
        _this.setData({
          input_reply: null
        })
        _this.onLoad()
      })
    }
  },

  getTime: function() {
    let time_now = util.formatTime(new Date());
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})